const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const multer = require('multer');
const storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, './files')
    },
    filename: function(req, file, cb) {
        cb(null, file.originalname)
    }
})
const fileFilter = function(req, file, cb) {
    if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
        req.notImg = true;
        return cb(new Error('Only image files are allowed!'), false);
    }
    cb(null, true);
};
// const storage = multer.memoryStorage()
const upload = multer({
    storage,
    fileFilter
})
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'imgFront.html'));
})
app.post('/upload', upload.single('photo'), (req, res) => {
    res.write(JSON.stringify(req.file.fieldname))
    // fs.writeFile('lll.png', req.file.buffer.data, function(err) {
    //     if (err) throw err;
    //     console.log('File saved.')
    // })
    res.end()
})
app.use(function(req, res, next) {
    res.status(404).sendFile(path.join(__dirname, 'error.html'))
})
app.listen(3000, () => {
    console.log('App listening on port 3000!')
})
// app.use(express.static('public'));